package mani.collections;

public class CustomHashMap<K, V> {
	private int size;
	private int DEFAULT_CAPACITY = 16;
	private MyEntry<K, V>[] mapValues = new MyEntry[DEFAULT_CAPACITY];

	public void put(K key, V value) {
		int hash = key.hashCode() % DEFAULT_CAPACITY;
		MyEntry<K, V> entry = mapValues[hash];
		if (entry != null) {
			boolean done = false;
			while (!done) {
				if(entry.getKey().equals(key)){
					entry.setValue(value);
					done = true;
				}else if(entry.next==null){
					entry.next = new MyEntry<K, V>(key, value);
					done= true;
				}else{
					entry = entry.next;
				}
			}
		}else{
			mapValues[hash] = new MyEntry<K, V>(key, value);
		}

	}
	
	public V get(K key){
		V value = null;
		int hash = key.hashCode()%DEFAULT_CAPACITY;
		MyEntry<K, V> entry = mapValues[hash];
		while(entry!=null && !entry.getKey().equals(key) ){
			entry = entry.next;
		}
		value = (entry!=null)?entry.getValue():null;		
		return value;
	}
	public static void main(String arg[]){
		CustomHashMap<String, String> hashMap = new CustomHashMap<>();
		hashMap.put("Shyam", "11");
		hashMap.put("mani", "34");
		hashMap.put("Shyam", "22");
		System.out.println(hashMap.get("Shyam"));
	}
}

class MyEntry<K, V> {
	private final K key;
	private V value;
	MyEntry next;

	public MyEntry(K key, V value) {
		this.key = key;
		this.value = value;
	}

	public V getValue() {
		return value;
	}

	public void setValue(V value) {
		this.value = value;
	}

	public K getKey() {
		return key;
	}

}